<?php

namespace App\Jobs\Train\Snap;

use App\Models\Task\SnapTasks\Repositories\Interfaces\SnapTaskRepositoryInterface;
use App\Models\Task\SnapTasks\SnapTask;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Log;

class ProcessUnlockWaitingConfirmTask implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    private SnapTask $task_info;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct(SnapTask $task)
    {
        $this->task_info = $task;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        //
        $task = $this->task_info->refresh();


        // 如果未确认 自动解锁
        if ($task->getTaskState() == SnapTask::TASK_STATE_WAITING_CONFIRM && $task->getSnapState() == SnapTask::SNAP_STATE_HAS_TICKET) {
            $taskRepo = app(SnapTaskRepositoryInterface::class);
            Log::info("解锁未开启自动出票的任务:{$task->getTaskId()},重置为抢票中");
            $taskRepo->updateTask($this->task_info, [
                'task_state' => SnapTask::TASK_STATE_SNAPPING,
                'snap_state' => SnapTask::SNAP_STATE_STARTED,
            ]);
        }

    }
}
